package com.freewater.springboot.demo.aop.apectj.injectdomain.domain;


import com.freewater.springboot.demo.aop.apectj.injectdomain.context.AuthContext;
import com.freewater.springboot.demo.aop.apectj.injectdomain.repository.AccountRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;

/**
 * 领域对象，这里假设需要在每次new对象时注入AccountRepository及AuthContext：
 * 1、每次new一个Account对象的时候自动注入AccountRepository(singleton)；
 * 2、每次new一个Account对象的时候自动注入AuthContext(prototype);
 * 另外这种被@Configurable注解的领域类，千万不要再使用SpringIOC容器管理（会造成重复初始化或者意外结果）；
 */
@Configurable(dependencyCheck = true, preConstruction = true)
public class Account {

    private String id;

    private String name;

    public Account(String id, String name) {
        this.id = id;
        this.name = name;
        // 在构造函数方法被调用前就初注入accountRepository（preConstruction = true 起的作用）
        final int allAccountAccount = accountRepository.findAllAccountAccount();
        System.out.printf("all account count: %s%n", allAccountAccount);
    }

    @Autowired
    private AccountRepository accountRepository;

    @Autowired
    private AuthContext authContext;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public AccountRepository getAccountRepository() {
        return accountRepository;
    }

    public void setAccountRepository(AccountRepository accountRepository) {
        this.accountRepository = accountRepository;
    }

    public AuthContext getAuthContext() {
        return authContext;
    }

    public void setAuthContext(AuthContext authContext) {
        this.authContext = authContext;
    }
}
